package com.acwing.partition13;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/2/28 9:58
 */
public class AC1232三体攻击 {

    private static final int N = 2000010;
    //s表示原三维数组映射后的一维数组，b为差分数组
    private static long[] s = new long[N], b = new long[N], temp = new long[N];
    //map记录差分数组和原数组进行转换的参数、偏移量
    private static int[][] map = {{0, 0, 0, 1}, {0, 0, 1, -1}, {0, 1, 0, -1}, {0, 1, 1, 1},
            {1, 0, 0, -1}, {1, 0, 1, 1}, {1, 1, 0, 1}, {1, 1, 1, -1}};
    private static int[][] op = new int[N >> 1][7];
    private static int p, q, r, m;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        p = Integer.parseInt(ss[0]);
        q = Integer.parseInt(ss[1]);
        r = Integer.parseInt(ss[2]);
        m = Integer.parseInt(ss[3]);
        ss = reader.readLine().split(" ");
        //输入原数组
        for (int i = 1, u = 0; i <= p; i++) {
            for (int j = 1; j <= q; j++) {
                for (int k = 1; k <= r; k++) {
                    s[get(i, j, k)] = Integer.parseInt(ss[u++]);
                }
            }
        }
        //输入所有的操作
        for (int i = 1; i <= m; i++) {
            ss = reader.readLine().split(" ");
            for (int j = 0; j < 7; j++) {
                op[i][j] = Integer.parseInt(ss[j]);
            }
        }
        //根据原数组得到差分数组
        for (int i = 1; i <= p; i++) {
            for (int j = 1; j <= q; j++) {
                for (int k = 1; k <= r; k++) {
                    for (int[] item : map) {
                        int x = i - item[0], y = j - item[1], z = k - item[2], t = item[3];
                        temp[get(i, j, k)] += s[get(x, y, z)] * t;
                    }
                }
            }
        }
        //二分寻找答案
        int l = 1, r = m;
        while (l < r) {
            int mid = l + r >> 1;
            if (check(mid)) r = mid;
            else l = mid + 1;
        }
        writer.write(l + "\n");
        writer.flush();
    }

    private static boolean check(int mid) {
        //在备份数组上进行操作
        System.arraycopy(temp, 0, b, 0, N);
        //枚举1-mid轮进攻，每次进攻都对三维空间内的战舰加上c的生命值，其中c<0
        for (int i = 1; i <= mid; i++) {
            int x1 = op[i][0], x2 = op[i][1], y1 = op[i][2], y2 = op[i][3], z1 = op[i][4], z2 = op[i][5], c = -op[i][6];
            b[get(x1, y1, z1)] += c;
            b[get(x1, y1, z2 + 1)] -= c;
            b[get(x1, y2 + 1, z1)] -= c;
            b[get(x1, y2 + 1, z2 + 1)] += c;
            b[get(x2 + 1, y1, z1)] -= c;
            b[get(x2 + 1, y1, z2 + 1)] += c;
            b[get(x2 + 1, y2 + 1, z1)] += c;
            b[get(x2 + 1, y2 + 1, z2 + 1)] -= c;
        }
        //根据差分数组求原数组，如果原数组有小于0的，就返回true
        Arrays.fill(s, 0);
        for (int i = 1; i <= p; i++) {
            for (int j = 1; j <= q; j++) {
                for (int k = 1; k <= r; k++) {
                    s[get(i, j, k)] += b[get(i, j, k)];
                    for (int u = 1; u < map.length; u++) {
                        int x = i - map[u][0], y = j - map[u][1], z = k - map[u][2], t = map[u][3];
                        s[get(i, j, k)] -= s[get(x, y, z)] * t;
                    }
                    if (s[get(i, j, k)] < 0) return true;
                }
            }
        }
        return false;
    }

    private static int get(int x, int y, int z) {
        return (x * q + y) * r + z;
    }
}
